package com.zy.demo018081.service.impl;

import com.zy.demo018081.pojo.po.DepartPo;
import com.zy.demo018081.repository.DepartRepository;
import com.zy.demo018081.service.DepartService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class DepartServiceImpl implements DepartService {

    private final DepartRepository departRepository;

    @Override
    public boolean save(DepartPo po) {
        DepartPo saved = departRepository.save(po);
        return saved != null;
    }

    @Override
    public boolean modify(DepartPo po) {
        DepartPo saved = departRepository.save(po);
        return saved != null;
    }

    @Override
    public boolean deleteById(Long id) {
        if (departRepository.existsById(id)) {
            departRepository.deleteById(id); // 如果ID不存在抛出异常
            return true;
        }
        return false;
    }

    @Override
    public DepartPo getById(Long id) {
        if (departRepository.existsById(id)) {
            return departRepository.getReferenceById(id);
        }
        return null;
    }

    @Override
    public List<DepartPo> getAll() {
        return departRepository.findAll();
    }
}
